package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.R;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    DictService dictService;
    @GetMapping("/findChildData/{id}")
    public R findChildData(@PathVariable long id){
        List<Dict> list=dictService.findChildData(id);
        return R.ok().data("list",list);
    }

    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }
    @PostMapping("/importData")
    public R importData(MultipartFile file) {
        dictService.importDictData(file);
        return R.ok();
    }
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable String parentDictCode,@PathVariable String value){
        String dictName = dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
        return dictName;
    }
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }

    @GetMapping(value = "/findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode){
        List<Dict> list=dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }
}
